package org.gameg.service.biz.impl;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gameg.Api.Service.ApiService;
import org.gameg.bean.BetPower;
import org.gameg.bean.Game;
import org.gameg.bean.Player;
import org.gameg.bean.Power;
import org.gameg.mapper.biz.TDrawMapper;
import org.gameg.mapper.biz.TGameMapper;
import org.gameg.model.biz.*;
import org.gameg.service.biz.*;
import org.gameg.service.biz.factory.GameFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.ibase4j.core.base.BaseService;
import top.ibase4j.core.exception.BusinessException;
import top.ibase4j.core.util.InstanceUtil;

/**
 * @author ShenHuaJie
 * @since 2018年4月21日 下午7:26:41
 */
@Service
@CacheConfig(cacheNames = "TGame")
public class GameServiceImpl extends BaseService<TGame, TGameMapper> implements IGameService {
    final Logger logger = LogManager.getLogger();
    static final String NIUNIU = "1";
    static final String BAIJIALE = "2";
    static final String DXDSH = "3";
    static final String TEMA = "4";
    @Autowired
    private IPlayerService playerService;
    @Autowired
    private IGamePlayerService gamePlayerService;
    @Autowired
    private IGamePowerService gamePowerService;
    @Autowired
    private ApiService apiService;
    private static ExecutorService executorService = Executors.newFixedThreadPool(50);

    @Transactional
    public void updateGameConfig(Game game) {
        TGame tGame = InstanceUtil.to(game, TGame.class);
        tGame = update(tGame);
        for (Power power : game.getPowers()) {
            TGamePower gamePower = InstanceUtil.to(power, TGamePower.class);
            gamePower.setGameId(tGame.getId());
            gamePowerService.update(gamePower);
        }
        for (Player player : game.getPlayers()) {
            TGamePlayer record = new TGamePlayer();
            record.setGameId(tGame.getId());
            record.setPlayerId(player.getId());
            record.setCreateBy(player.getId());
            if ("1".equals(game.getGameRule())) {
                if ("Y".equals(player.getIsZj())) {
                    record.setIsZj("Y");
                } else {
                    throw new BusinessException("只有庄家才能提交游戏配置.");
                }
            }
            gamePlayerService.update(record);
            tGame.setCreateBy(player.getId());
            update(tGame);
        }
    }

    @Transactional
    public void addGamePlayer(Player player) {
        gamePlayerService.update(InstanceUtil.to(player, TGamePlayer.class));
        executorService.execute(new Runnable() {
            @Override
            public void run() {

            }
        });
    }

    /**
     * 规则：有庄、无庄、比门
     * 模式：喝水模式、纯算账模式、积分模式
     * 玩法：一位、两位、三位（在pointHelper中。）
     *
     * @param game 配置
     * @return
     */
    @Override
    @Transactional
    public List<TGamePlayer> updateGameResult(Game game) {
        List<TGamePlayer> resultList = InstanceUtil.newArrayList(); //玩家结果列表
        initPlayer(game);
        PlayerHelper.initPoker(game.getPlayers());
        GuessSize.initBetPower(game.getPlayers());
        GuessSize.initSize(game.getPlayers());
        GameModelMethod gameModelMethod = null;//游戏模式借口。根据不同的模式，进行不同的游戏
        drinkModel(resultList, game);
        return resultList;
    }

    public void drinkModel(List<TGamePlayer> resultList, Game game) {
        //在判断一次游戏模式。
        //boolean isUpdate = "1".equals(game.getGameMode());
        boolean isUpdate = true;
        String gameRule = game.getGameRule();//  游戏规则
        Player zh = null;
        List<Player> xians = null;
        switch (game.getGameRule()) { //根据不同的 判断条件创建不同的容器。
            case "1"://有庄
                xians = InstanceUtil.newArrayList();
                break;
            case "2"://无庄
                break;
            case "3"://比门
                break;
        }
        // 赔率
        Map<Integer, Power> powers = InstanceUtil.newHashMap();
        for (Power power : game.getPowers()) {
            if (power.getIsOpen() == null) {
                power.setIsOpen(false);   // 设置是否特殊排序，默认为不开启特殊排序
            }
            if (power.getPoint() != null || "".equals(power.getPoint())) {
                powers.put(power.getPoint(), power);
            } else {
                powers.put(power.getBetPoint(), power);
            }
        }
        // 超时
        boolean mainTimeout = false;     //  默认为没有超时
        TGame tGame = InstanceUtil.to(game, TGame.class);
        tGame = update(tGame);
        List<Player> qbs = InstanceUtil.newArrayList();  //抢错包玩家
        List<Player> players = InstanceUtil.newArrayList();//押注玩家
        List<Player> playerList = InstanceUtil.newArrayList();

        List<Player> winXPlayerList = InstanceUtil.newArrayList();// 获胜玩家闲家容器
        List<Player> loserXPlayerList = InstanceUtil.newArrayList(); //失败玩家闲家容器
        for (Player player : game.getPlayers()) {
            if ("1".equals(gameRule) && !"Y".equals(player.getIsZj())) {//有庄，为闲用户
                if ((player.getBet() == null || player.getBet() == 0) && player.getAmount() != null
                        && player.getAmount().doubleValue() > 0 && player.getTime() != null && player.getTime() > 0) {//没有押注但是抢红包了
                    if (game.getPunishScore() != null && game.getPunishScore() > 0) {
                        player.setPoint(PointHelper.getPoint(game, powers, player.getAmount()));
                        player.setPower(powers.get(player.getPoint()).getPower());
                        player.setNiuPoint(powers.get(player.getPoint()).getNiuPoint());
                        if (player.getTime() < game.getPlayerTime()) { // 抢错包处理
                            updatePlayerScore(tGame.getId(), player, resultList, -game.getPunishScore(), isUpdate);
                        } else {
                            updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                        }
                    }
                    qbs.add(player);
                } else {
                    players.add(player);
                }
            } else if ("1".equals(gameRule) && "Y".equals(player.getIsZj())) {//有庄，为庄用户
                zh = player;
                if ("2".equals(game.getOneFen()) && zh.getAmount().doubleValue() == 0.01) {
                    throw new RuntimeException("当局游戏不算");
                }
                players.add(player);
            }
            if (player.getAmount() != null && player.getAmount().doubleValue() > 0 && player.getTime() != null
                    && player.getTime() > 0) {
                playerList.add(player);
            }
        }
        Collections.sort(qbs, new Comparator<Player>() {
            @Override
            public int compare(Player o1, Player o2) {
                return o1.getTime() > o2.getTime() ? 1 : o1.getTime() < o2.getTime() ? -1 : 0;
            }
        });
        Collections.sort(playerList, new Comparator<Player>() {
            @Override
            public int compare(Player o1, Player o2) {
                return o1.getTime() > o2.getTime() ? -1 : o1.getTime() < o2.getTime() ? 1 : 0;
            }
        });
        Collections.sort(players, new Comparator<Player>() {
            @Override
            public int compare(Player o1, Player o2) {
                return o1.getTime() > o2.getTime() ? 1 : o1.getTime() < o2.getTime() ? -1 : 0;
            }
        });
        boolean isZhFirst = "1".equals(gameRule) && "Y".equals(players.get(0).getIsZj());  //庄家
        Collections.sort(players, new Comparator<Player>() {
            @Override
            public int compare(Player o1, Player o2) {
                return o1.getBetNo() > o2.getBetNo() ? 1 : o1.getBetNo() < o2.getBetNo() ? -1 : 0;
            }
        });
        int zhScore = 0, q = 0;
        for (int i = 0; i < players.size(); i++) { //遍历押注玩家
            Player player = players.get(i);
            if (player.getBet() != null && player.getBet() > 0) {
                // 无包处理
                if (player.getAmount() == null || player.getAmount().doubleValue() == 0) {
                    if ("1".equals(gameRule) && "Y".equals(player.getIsZj())) { // 庄家无包
                        if ("Y1".equals(player.getIsExe())) {
                            player.setTime(playerList.get(0).getTime());
                            player.setAmount(playerList.get(0).getAmount());
                        } else if ("Y2".equals(player.getIsExe())) {
                            player.setTime(playerList.get(1).getTime());
                            player.setAmount(playerList.get(1).getAmount());
                        } else {
                            switch (game.getMainNone()) {
                                case "1":
                                    player.setTime(playerList.get(0).getTime());
                                    player.setAmount(playerList.get(0).getAmount());
                                    break;
                                case "2":
                                    player.setTime(playerList.get(1).getTime());
                                    player.setAmount(playerList.get(1).getAmount());
                                    break;
                                case "3":
                                    player.setTime(qbs.get(q).getTime());
                                    player.setAmount(qbs.get(q).getAmount());
                                    break;
                            }
                            player.setProxy(!(qbs.get(q++).getTime() <= game.getMainTime()));// 玩家是否被代抢
                        }
                    } else if ("1".equals(gameRule)) { // 闲家无包
                        player.setProxy(!(qbs.get(q++).getTime() <= game.getPlayerTime()));
                        if ("Y".equals(player.getIsExe())) {
                            player.setTime(playerList.get(0).getTime());
                            player.setAmount(playerList.get(0).getAmount());
                            xians.add(player);
                        } else {
                            switch (game.getPlayerNone()) {
                                case "1":
                                    player.setTime(playerList.get(0).getTime());
                                    player.setAmount(playerList.get(0).getAmount());
                                    xians.add(player);
                                    break;
                                case "2":
                                    player.setTime(playerList.get(1).getTime());
                                    player.setAmount(playerList.get(1).getAmount());
                                    xians.add(player);
                                    break;
                                case "3":
                                case "4":
                                    player.setPoint(PointHelper.getPoint(game, powers, player.getAmount()));
                                    player.setNiuPoint(powers.get(player.getPoint()).getNiuPoint());
                                    player.setPower(powers.get(player.getPoint()).getPower());
                                    if (!isZhFirst || "3".equals(game.getMainFirst())) {
                                        if ("3".equals(game.getPlayerNone())) {
                                            logger.warn("闲家{}无包全陪，庄家{}赢", player.getWxId(), zh.getWxId());
                                            player.setPower(zh.getPower());
                                            double score = 0;
                                            if (player.isPoker()) {//该闲家是否梭哈
                                                score = player.getBet();
                                                player.setPower((double)1);
                                            } else if (!player.isPoker()) {
                                                score = player.getBet() * zh.getPower();
                                            }
                                            //int score = player.getBet() * zh.getPower();
                                            zh.setOrgCreditScore((int)(zh.getOrgCreditScore() + score));
                                            updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                                        } else if ("4".equals(game.getPlayerNone())) {
                                            logger.warn("闲家{}无包平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                                            player.setPower((double)1);
                                            int score = 0;
                                            if (player.isPoker()) {//该闲家是否梭哈
                                                score = player.getBet();
                                                player.setPower((double)1);
                                            } else if (!player.isPoker()) {
                                                score = player.getBet();
                                            }
                                            //int score = player.getBet();
                                            zh.setOrgCreditScore(zh.getOrgCreditScore() + score);
                                            updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                                        }
                                    } else {
                                        xians.add(player);
                                    }
                                    continue;
                            }
                        }
                    }
                } else if ("1".equals(gameRule) && !"Y".equals(player.getIsZj())) {
                    xians.add(player);
                }
                player.setPoint(PointHelper.getPoint(game, powers, player.getAmount()));
                //验证集齐奖励，数字奖励，连赢奖励。
                apiService.aword(player);

                player.setNiuPoint(powers.get(player.getPoint()).getNiuPoint());
                if (player.getPlayerGameType().equals(NIUNIU)) {//牛牛
                    player.setPower(powers.get(player.getPoint()).getPower());
                    player.setBetSum(powers.get(player.getPoint()).getBetSum());
                    player.setXianWinDrow(powers.get(player.getPoint()).getXianWinDrow());
                    player.setWinGdDrow(powers.get(player.getPoint()).getWinGdDrow());
                } else if (player.getPlayerGameType().equals(BAIJIALE)) {//百家乐
                    player.setPower(powers.get(player.getBetPoint()).getPower());
                } else if (player.getPlayerGameType().equals(DXDSH)) {//大小单双合
                    player.setPower(powers.get(Integer.valueOf(player.getGuessSize())).getPower());
                } else {//特码
                    for (BetPower betPower :
                            player.getBetPowerList()) {
                        betPower.setPower(powers.get(betPower.getBetPoint()).getPower());
                    }
                    player.setPower(player.getBetPowerList().get(0).getPower());
                }
            }
        }
        if ("1".equals(gameRule) && zh == null) {
            throw new BusinessException("缺少庄家");
        }
        mainTimeout = zh.getTime() >= game.getMainTime();
        String gameType = game.getGameType(); // 游戏类型
        if ("1".equals(gameRule)) { // 有庄模式
            for (Player player : xians) {
                switch (player.getPlayerGameType()) {
                    case NIUNIU:
                        zhScore = playNiuNiu(game, player, zh, tGame, resultList, isUpdate, isZhFirst, zhScore, winXPlayerList, mainTimeout, loserXPlayerList, powers);
                        break;
                    case BAIJIALE:
                        zhScore = playBJL(game, player, zh, tGame, resultList, isUpdate, isZhFirst, zhScore, winXPlayerList, mainTimeout, loserXPlayerList, powers);
                        break;
                    case DXDSH:
                        zhScore = playDXDSH(game, player, zh, tGame, resultList, isUpdate, isZhFirst, zhScore, winXPlayerList, mainTimeout, loserXPlayerList, powers);
                        break;
                    case TEMA:
                        zhScore = playTeMa(game, player, zh, tGame, resultList, isUpdate, isZhFirst, zhScore, winXPlayerList, mainTimeout, loserXPlayerList, powers);
                        break;
                }
            }
            Collections.sort(resultList, new Comparator<TGamePlayer>() {
                @Override
                public int compare(TGamePlayer o1, TGamePlayer o2) {
                    return o1.getTime() > o2.getTime() ? 1 : o1.getTime() < o2.getTime() ? -1 : 0;
                }
            });
            if (isUpdate) {
                zh.setXianWinScore(zhScore);
                //updatePlayerScore(zh, zhScore);
            }
            //判断是否是喝水模式
            zhScore = drinking(game, zh, playerList, tGame, zhScore, winXPlayerList, loserXPlayerList, isUpdate, resultList);
            Collections.sort(resultList, new Comparator<TGamePlayer>() {
                @Override
                public int compare(TGamePlayer o1, TGamePlayer o2) {
                    return o1.getCreditScore() > o2.getCreditScore() ? 1 : o1.getCreditScore() < o2.getCreditScore() ? -1 : 0;
                }
            });
            int zhScorez=0;
            if (zh.getOrgCreditScore() < 0&&game.getGameMode().equals("1")) {//喝水模式，并庄钱已经输光。有玩家喝水。
                zhScorez= zhScore-zh.getOrgCreditScore();
            }
            updatePlayerScore(zh, zhScorez);
            TGamePlayer gamePlayer = InstanceUtil.to(zh, TGamePlayer.class);//庄家列表
            gamePlayer.setGameId(tGame.getId());
            gamePlayer.setCreditScore(zhScore);
            gamePlayer.setNewCreditScore(gamePlayer.getOrgCreditScore() + zhScore);
            gamePlayer.setOrgCreditScorez(gamePlayer.getOrgCreditScore() + zhScorez);
            gamePlayerService.update(gamePlayer);
            resultList.add(0, gamePlayer);
        } else if ("2".equals(gameRule)) { // 无庄模式
            switch (gameType) {
                case NIUNIU:
                    Player player1 = game.getPlayers().get(0);
                    Player player2 = game.getPlayers().get(1);
                    int result = PointHelper.compare(player1, player2, game);
                    TGamePlayer gamePlayer1 = InstanceUtil.to(player1, TGamePlayer.class);
                    TGamePlayer gamePlayer2 = InstanceUtil.to(player2, TGamePlayer.class);
                    if (result == 1) { // 玩家1赢
                        logger.warn("玩家{}赢，玩家{}赢输", player1.getWxId(), player2.getWxId());
                        double score = player2.getBet() * powers.get(player1.getPoint()).getPower();
                        if (isUpdate) {
                            updatePlayerScore(player1, (int)score);
                            updatePlayerScore(player2, (int)-score);
                        }
                        gamePlayer1.setCreditScore((int)score);
                        gamePlayer2.setCreditScore((int)-score);
                    } else if (result == -1) { // 玩家2赢
                        logger.warn("玩家{}赢，玩家{}赢输", player2.getWxId(), player1.getWxId());
                        double score = player1.getBet() * powers.get(player2.getPoint()).getPower();
                        if (isUpdate) {
                            updatePlayerScore(player1, (int)-score);
                            updatePlayerScore(player2, (int)score);
                        }
                        gamePlayer1.setCreditScore((int)-score);
                        gamePlayer2.setCreditScore((int)score);
                    } else { // 打平
                        logger.warn("玩家{}和玩家{}打平", player1.getWxId(), player2.getWxId());
                        gamePlayer1.setCreditScore(0);
                        gamePlayer2.setCreditScore(0);
                    }
                    gamePlayer1.setGameId(tGame.getId());
                    gamePlayerService.update(gamePlayer1);
                    resultList.add(gamePlayer1);
                    gamePlayer2.setGameId(tGame.getId());
                    gamePlayerService.update(gamePlayer2);
                    resultList.add(gamePlayer2);
                    break;
                case BAIJIALE:

                    break;
                case DXDSH:

                    break;
                case TEMA:

                    break;
            }
        }
    }

    public void initPlayer(Game game){
        for (Player p :
                game.getPlayers()) {
            p.setOrgCreditScorez(p.getOrgCreditScore());
            if (p.getIsZj().equals("N")) {
                //p.setOrgCreditScore(game.getUpZCreditScore());
                p.setOrgCreditScore(5000);
            }
        }
    }


    /**
     * 测试研究区
     *
     * @param args
     */
    public static void main(String[] args) {
        //降序排列
        /*Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1>o2?-1:o1<o2?1:0;
            }
        });*/
        String betStr = "135买200";//135买200,257买900
        String[] betStrList = betStr.split("，");
        for (int i = 0; i < betStrList.length; i++) {

        }

    }


    public void updatePlayerScore(Player player, Integer score) {
        TPlayer param = InstanceUtil.to(player, TPlayer.class);
        TPlayer record = playerService.selectOne(param);
        if (record != null) {
            record.setCreditScore(record.getCreditScore() + score);
            playerService.update(record);
        }
    }

    public void updatePlayerScore(Player player, Integer score, Integer scorez) {
        TPlayer param = InstanceUtil.to(player, TPlayer.class);
        TPlayer record = playerService.selectOne(param);
        if (record != null) {
            record.setCreditScore(record.getCreditScore() + score);
            record.setCreditScore(record.getCreditScore() + scorez);
            playerService.update(record);
        }
    }

    public void updatePlayerScore(Long gameId, Player player, List<TGamePlayer> resultList, int score,
                                  boolean isUpdate) {
        if (isUpdate) {
            updatePlayerScore(player, score);
        }
        TGamePlayer gamePlayer = InstanceUtil.to(player, TGamePlayer.class);
        gamePlayer.setCreditScore(score);
        gamePlayer.setGameId(gameId);
        gamePlayer.setNewCreditScore(gamePlayer.getOrgCreditScore() + score);
        gamePlayerService.update(gamePlayer);
        resultList.add(gamePlayer);
    }

    public void updatePlayerScore(Long gameId, Player player, List<TGamePlayer> resultList, int score,
                                  boolean isUpdate, boolean isToDrink) {
        if (isUpdate) {
            updatePlayerScore(player, score);
        }
        TGamePlayer gamePlayer = InstanceUtil.to(player, TGamePlayer.class);
        gamePlayer.setDrink(isToDrink);
        gamePlayer.setCreditScore(score);
        gamePlayer.setGameId(gameId);
        gamePlayer.setNewCreditScore(gamePlayer.getOrgCreditScore() + score);
        gamePlayerService.update(gamePlayer);
        resultList.add(gamePlayer);
    }

    public Integer drinking(Game game, Player zh, List<Player> playerList, TGame tGame, Integer zhScore, List<Player> winXPlayerList, List<Player> loserXPlayerList,
                         boolean isUpdate, List<TGamePlayer> resultList) {
        Integer zScore = zhScore;
        //喝水操作开始
        boolean isDrink = false;
        if (game.getGameMode().equals("1")) {
            isDrink = (zh.getOrgCreditScore() + zhScore) < 0;
        }
        //初始化庄家胜利后的总剩余金额
        for (Player player :
                loserXPlayerList) {
            zh.setOrgCreditScore(zh.getOrgCreditScore() + player.getXianWinScore());
            zh.setOrgCreditScorez(zh.getOrgCreditScorez() + player.getXianWinScore());
        }
        if (isDrink && game.getGameMode().equals("1")) {
            switch (game.getDrinkSetting()) {//给获胜玩家 按照设置规则排序。
                case "1"://倍率，金额，牛数，抢包时间
                    Collections.sort(winXPlayerList, new Comparator<Player>() {
                        @Override
                        public int compare(Player o1, Player o2) {
                            if (o1.getPower() > o2.getPower()) {
                                return -1;
                            } else if (o1.getPower() < o2.getPower()) {
                                return 1;
                            } else {
                                if (o1.getAmount().doubleValue() > o2.getAmount().doubleValue()) {
                                    return -1;
                                } else if (o1.getAmount().doubleValue() < o2.getAmount().doubleValue()) {
                                    return 1;
                                } else {
                                    if (o1.getNiuPoint() > o2.getNiuPoint()) {
                                        return -1;
                                    } else if (o1.getNiuPoint() < o2.getNiuPoint()) {
                                        return 1;
                                    } else {
                                        return o1.getTime() > o2.getTime() ? 1 : o1.getTime() < o2.getTime() ? -1 : 0;
                                    }
                                }

                            }
                            //return o1.getPower()>o2.getPower()?-1:o1.getPower()<o2.getPower()?1:0;
                        }
                    });
                    break;
                case "2"://倍率，牛数，金额，抢包时间
                    Collections.sort(winXPlayerList, new Comparator<Player>() {
                        @Override
                        public int compare(Player o1, Player o2) {
                            if (o1.getPower() > o2.getPower()) {
                                return -1;
                            } else if (o1.getPower() < o2.getPower()) {
                                return 1;
                            } else {
                                if (o1.getNiuPoint() > o2.getNiuPoint()) {
                                    return -1;
                                } else if (o2.getNiuPoint() < o2.getNiuPoint()) {
                                    return 1;
                                } else {
                                    if (o1.getAmount().doubleValue() > o2.getAmount().doubleValue()) {
                                        return -1;
                                    } else if (o1.getAmount().doubleValue() < o2.getAmount().doubleValue()) {
                                        return 1;
                                    } else {
                                        return o1.getTime() > o2.getTime() ? 1 : o1.getTime() < o2.getTime() ? -1 : 0;
                                    }
                                }

                            }
                        }
                    });
                    break;
                case "3"://抢包时间
                    Collections.sort(winXPlayerList, new Comparator<Player>() {
                        @Override
                        public int compare(Player o1, Player o2) {
                            if (o1.getPower() > o2.getPower()) {
                                return -1;
                            } else if (o1.getPower() < o2.getPower()) {
                                return 1;
                            } else {
                                return o1.getTime() > o2.getTime() ? 1 : o1.getTime() < o2.getTime() ? -1 : 0;
                            }
                        }
                    });
                    break;
            }
            //排序结束。 开始遍历算账。
            Integer zhOrgCreditScore = zh.getOrgCreditScore();
            for (Player player :
                    winXPlayerList) {
                boolean isToDrink = (zhOrgCreditScore - player.getXianWinScore()) < 0;
                if (isToDrink) {
                    updatePlayerScore(tGame.getId(), player, resultList, zhOrgCreditScore, isUpdate, isToDrink);
                    zhOrgCreditScore = 0;
                } else {
                    updatePlayerScore(tGame.getId(), player, resultList, player.getXianWinScore(), isUpdate, isToDrink);
                    zhOrgCreditScore -= player.getXianWinScore();
                }
            }
        } else {
            for (Player player :
                    winXPlayerList) {
                Integer xScore = player.getXianWinScore();
                if (game.isTDraw()) {
                    /*if (game.gettXianGdDraw() > 0) {//固定抽水开启
                        xScore -= (int) game.gettXianGdDraw();
                    }*/
                    if (game.isTXianWinDraw()) {
                        xScore -= (int) (player.getXianWinDrow() * xScore / 100);
                        insertTDraw("闲赢百分比抽水", (double)(int) (player.getXianWinDrow() * xScore / 100), player.getPlayerId(), player.getPlayerGameUser());
                    }
                    if (game.istZWinGdDraw() ) {
                        xScore -= (int)player.getWinGdDrow();
                        insertTDraw("闲赢固定抽水", (double)(int)player.getWinGdDrow(), player.getPlayerId(), player.getPlayerGameUser());
                    }
                    player.setDrowPrice(player.getXianWinScore()-xScore);
                }
                //updatePlayerScore(tGame.getId(), player, resultList, player.getXianWinScore(), isUpdate, false);
                updatePlayerScore(tGame.getId(), player, resultList, xScore, isUpdate, false);
            }
        }
        //初始化庄家胜利后的总剩余金额
        for (Player player :
                loserXPlayerList) {
            zh.setOrgCreditScore(zh.getOrgCreditScore() - player.getXianWinScore());
            zh.setOrgCreditScorez(zh.getOrgCreditScorez() - player.getXianWinScore());
        }
        //喝水操作结束
        if (game.isTDraw()) {//抽水模式开启 // 没有上庄抽水，和续庄抽水 。
            if (game.gettZWinDraw() > 0 && zScore > 0) {//庄赢抽水
                zScore -= (int)(zhScore*game.gettZWinDraw()/100);
                insertTDraw("庄赢抽水", (double)(int)(zhScore*game.gettZWinDraw()/100), zh.getPlayerId(), zh.getPlayerGameUser());
            }
            if (game.gettBetPlayerSum() > 0) {
                zScore-= (int)(game.gettBetPlayerSum()*(playerList.size()-1));
                insertTDraw("按下注人数抽水", (double)(int)(game.gettBetPlayerSum()*(playerList.size()-1)), zh.getPlayerId(), zh.getPlayerGameUser());
            }
            if (game.getTfjcs() > 0) {
                zScore-= (int)game.getTfjcs();
                insertTDraw("附加抽水", (double)(int)game.getTfjcs(), zh.getPlayerId(), zh.getPlayerGameUser());
            }
            if (game.gettBetSum() > 0) {
                int bet = 0;
                for (Player p :
                        playerList) {
                    if (p.getIsZj().equals("N")) {//闲家
                        bet += p.getBet();
                    }
                }
                zScore -= (int)(bet/game.gettBetSum()*zh.getBetSum());
                insertTDraw("按下注注数抽水", (double)(int)(bet/game.gettBetSum()*zh.getBetSum()), zh.getPlayerId(), zh.getPlayerGameUser());
            }
            /*if (game.istZWinGdDraw() ) {
                zScore -= (int)zh.getWinGdDrow();
            }*/
            if (game.gettZGdDraw() > 0) {
                zScore -= (int) ((playerList.size() - 1) * game.gettZGdDraw());
                insertTDraw("庄固定扣费（人数*扣费值）", (double)(int)((playerList.size() - 1) * game.gettZGdDraw()), zh.getPlayerId(), zh.getPlayerGameUser());
            }
            if (game.gettPtDraw() > 0) {
                int zBet = 0;
                for (Player p :
                        playerList) {
                    if (p.getIsZj().equals("N") && !p.isPoker()) {//普通闲家
                        zBet += p.getXianWinScore();
                    }
                }
                zScore -= (int) (zBet * game.gettPtDraw() / 100);
                insertTDraw("普通抽水", (double)(int)(zBet * game.gettPtDraw() / 100), zh.getPlayerId(), zh.getPlayerGameUser());
            }

            if (game.gettPokerDraw() > 0) {
                int zBet = 0;
                for (Player p :
                        playerList) {
                    if (p.getIsZj().equals("N")&&p.isPoker()) {//梭哈闲家
                        zBet += p.getXianWinScore();
                    }
                }
                zScore -= (int) (zBet * game.gettPokerDraw() / 100);
                insertTDraw("梭哈抽水：当局梭哈总下注*抽水值", (double)(int)(zBet * game.gettPokerDraw() / 100), zh.getPlayerId(), zh.getPlayerGameUser());
            }
            zh.setDrowPrice(zhScore-zScore);
        }
        return zScore;
    }

    @Autowired
    private TDrawMapper tDrawMapper;

    public void insertTDraw(String type,Double price,Long playerId,Long gameUserId){
        TDraw tDraw = new TDraw();
        tDraw.setGameUserId(gameUserId);
        tDraw.setPlayerId(playerId);
        tDraw.setPrice(price);
        tDraw.setType(type);
        tDraw.setCreateTime(new Date());
        tDrawMapper.insert(tDraw);
    }

    public Integer playDXDSH(Game game, Player player, Player zh, TGame tGame, List<TGamePlayer> resultList, boolean isUpdate, boolean isZhFirst, int zhScore, List<Player> winXPlayerList, boolean mainTimeout, List<Player> loserXPlayerList, Map<Integer, Power> powers) {
        Integer zjscore = zhScore;
        if ("2".equals(game.getOneFen()) && player.getAmount().doubleValue() == 0.01) {
            logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
            updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
            return zjscore;
        }
        if (isZhFirst && "1".equals(game.getMainFirst())) {
            logger.warn("闲家{}赢，庄家{}头包全赔", player.getWxId(), zh.getWxId());
            double score = 0;
            if (player.isPoker()) {//该闲家梭哈，只赔 百分比
                score = (int) (player.getBet() * Double.valueOf("0." + game.getPoker()));
            } else if (!player.isPoker()) {
                score = player.getBet() * player.getPower();
            }
            //int score = player.getBet() * player.getPower();
            zjscore -= (int)score;
            player.setXianWinScore((int)score);
            winXPlayerList.add(player);
            //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
        } else if (isZhFirst && "2".equals(game.getMainFirst())) {
            logger.warn("闲家{}赢，庄家{}头包平赔", player.getWxId(), zh.getWxId());
            int score = 0;
            if (player.isPoker()) {//该闲家梭哈，只赔 百分比
                score = (int) (player.getBet() * Double.valueOf("0." + game.getPoker()));
            } else if (!player.isPoker()) {
                score = player.getBet();
            }
            //int score = player.getBet();
            zjscore -= score;
            player.setXianWinScore(score);
            winXPlayerList.add(player);
            //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
        } else {
            // 超时处理
            if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy() && game.isZfreeBuy() && game.isXfreeBuy()) {
                logger.warn("闲家{}和庄家{}都超时", player.getWxId(), zh.getWxId());   //两家在自买的时候 ，判定都开启才能判定为两家都超时
                if ("1".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                } else if ("2".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower((double)1);
                    } else if (!player.isPoker()) {
                        //score = player.getBet() * zh.getPower();
                        score = player.getBet();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower((double)1);
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower((double)1);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet() * 1;
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                }
                return zjscore;
            } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy() && !game.isZfreeBuy() && game.isXfreeBuy()) {
                logger.warn("闲家{}开启超时判定，庄家{}没有开启超时判定，闲家超时", player.getWxId(), zh.getWxId());   //两家在自买的时候 ，判定都开启才能判定为两家都超时
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower((double)1);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        //score = player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower((double)1);
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower((double)1);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy() && game.isZfreeBuy() && !game.isXfreeBuy()) {
                logger.warn("闲家{}闲家没有开启超时判定，庄家{}开启超时判定，庄家超时", player.getWxId(), zh.getWxId());   //两家在自买的时候 ，判定都开启才能判定为两家都超时
                if ("1".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}全赔", player.getWxId(), zh.getWxId());
                    double score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower((double)1);
                    } else if (!player.isPoker()) {
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * player.getPower();
                    zjscore -= (int)score;
                    player.setXianWinScore((int)score);
                    winXPlayerList.add(player);
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("2".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}平赔", player.getWxId(), zh.getWxId());
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower((double)1);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore -= score;
                    player.setXianWinScore(score);
                    winXPlayerList.add(player);
                    ;
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("3".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                }
                if (!"4".equals(game.getMainTimeout())) {
                    return zjscore;
                }
            } else if (mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy()&& game.isZfreeBuy()) {
                logger.warn("庄家{}超时", zh.getWxId());
                if ("1".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}全赔", player.getWxId(), zh.getWxId());
                    double score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower((double)1);
                    } else if (!player.isPoker()) {
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * player.getPower();
                    zjscore -= (int)score;
                    player.setXianWinScore((int)score);
                    winXPlayerList.add(player);
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("2".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}平赔", player.getWxId(), zh.getWxId());
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower((double)1);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore -= score;
                    player.setXianWinScore(score);
                    winXPlayerList.add(player);
                    ;
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("3".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                } else if ("5".equals(game.getMainTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == 1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                    } else {
                        logger.warn("闲家{}赢，庄家{}输", player.getWxId(), zh.getWxId());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower((double)1);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            score = player.getBet() * player.getPower();
                        }
                        //int score = player.getBet() * player.getPower();
                        zjscore -= (int)score;
                        player.setXianWinScore((int)score);
                        winXPlayerList.add(player);
                        //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                    }
                }
                if (!"4".equals(game.getMainTimeout())) {
                    return zjscore;
                }
            } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && game.isXfreeBuy()) {
                logger.warn("闲家{}超时", player.getWxId());
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower((double)1);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        //score = player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower((double)1);
                    int score = 0;
                    if (player.isPoker()) {
                        player.setPower((double)1);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if ("5".equals(game.getPlayerTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == -1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                    } else {
                        logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                        //player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower((double)1);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            //score = player.getBet() * zh.getPower();
                            score = player.getBet() * player.getPower();
                        }
                        //int score = player.getBet() * zh.getPower();
                        zjscore += (int)score;
                        player.setXianWinScore((int)score);
                        loserXPlayerList.add(player);
                        updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    }
                }
                if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            } else if (player.isProxy() && "N".equals(player.getIsExe()) && game.isXfreeBuy()) {
                logger.warn("闲家{}超时", player.getWxId());
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower((double)1);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        //score =  player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower((double)1);
                    int score = 0;
                    if (player.isPoker()) {
                        player.setPower((double)1);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += score;
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if ("5".equals(game.getPlayerTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == -1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                    } else {
                        logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                        //player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower((double)1);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            //score = player.getBet() * zh.getPower();
                            score = player.getBet() * player.getPower();
                        }
                        // int score = player.getBet() * zh.getPower();
                        zjscore += (int)score;
                        updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    }
                }
                if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            }
            // 正常情况
            int result = PointHelper.compare(game, player);
            if (result == 1) { // 庄赢
                logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                //player.setPower(zh.getPower());
                double score = 0;
                if (player.isPoker()) {
                    player.setPower((double)1);
                    score = player.getBet();
                } else if (!player.isPoker()) {
//                        score = player.getBet() * zh.getPower();
                    score = player.getBet();
                }
                //int score = player.getBet() * zh.getPower();
                zjscore += (int)score;
                player.setXianWinScore((int)score);
                loserXPlayerList.add(player);
                updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
            } else if (result == -1) { // 闲赢
                logger.warn("闲家{}赢，庄家{}输", player.getWxId(), zh.getWxId());
                double score = 0;
                if (player.isPoker()) {
                    player.setPower((double)1);
                    score = player.getBet();
                } else if (!player.isPoker()) {
                    score = player.getBet() * player.getPower();
                }
                //int score = player.getBet() * player.getPower();
                zjscore -= (int)score;
                player.setXianWinScore((int)score);
                winXPlayerList.add(player);
                //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
            } else { // 玩家抓合
                logger.warn("玩家{}抓合", player.getWxId());//1.抓合不赔；2.抓合赔一半；3.抓合全赔
                if (game.getCatchUs().equals("1")) {//抓合不赔
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if (game.getCatchUs().equals("2")) {//抓合赔一半
                    logger.warn("闲家{}输，庄家{}赢，赔一半", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    score = player.getBet() * player.getPower() / 2;
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if (game.getCatchUs().equals("3")) {//抓合全赔
                    logger.warn("闲家{}输，庄家{}赢，全赔", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower((double)1);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
//                        score = player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                }


            }
        }
        return zjscore;
    }

    public Integer playNiuNiu(Game game, Player player, Player zh, TGame tGame, List<TGamePlayer> resultList, boolean isUpdate, boolean isZhFirst, int zhScore, List<Player> winXPlayerList, boolean mainTimeout, List<Player> loserXPlayerList, Map<Integer, Power> powers) {
        Integer zjscore = zhScore;
        if ("2".equals(game.getOneFen()) && player.getAmount().doubleValue() == 0.01) {
            logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
            updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
            return zjscore;
        }
        if (isZhFirst && "1".equals(game.getMainFirst())) {
            logger.warn("闲家{}赢，庄家{}头包全赔", player.getWxId(), zh.getWxId());
            double score = 0;
            if (player.isPoker()) {//该闲家梭哈，只赔 百分比
                score = (int) (player.getBet() * Double.valueOf("0." + game.getPoker()));
            } else if (!player.isPoker()) {
                score = player.getBet() * player.getPower();
            }
            //int score = player.getBet() * player.getPower();
            zjscore -= (int)score;
            player.setXianWinScore((int)score);
            winXPlayerList.add(player);
            //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
        } else if (isZhFirst && "2".equals(game.getMainFirst())) {
            logger.warn("闲家{}赢，庄家{}头包平赔", player.getWxId(), zh.getWxId());
            int score = 0;
            if (player.isPoker()) {//该闲家梭哈，只赔 百分比
                score = (int) (player.getBet() * Double.valueOf("0." + game.getPoker()));
            } else if (!player.isPoker()) {
                score = player.getBet();
            }
            //int score = player.getBet();
            zjscore -= score;
            player.setXianWinScore(score);
            winXPlayerList.add(player);
            //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
        } else {
            // 超时处理
            if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy()) {
                logger.warn("闲家{}和庄家{}都超时", player.getWxId(), zh.getWxId());
                if ("1".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                } else if ("2".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower((double)1);
                    } else if (!player.isPoker()) {
                        score = player.getBet() * zh.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("3".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower((double)1);
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower((double)1);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet() * 1;
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                }
                return zjscore;
            } else if (mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy()) {
                logger.warn("庄家{}超时", zh.getWxId());
                if ("1".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}全赔", player.getWxId(), zh.getWxId());
                    double score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower((double)1);
                    } else if (!player.isPoker()) {
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * player.getPower();
                    zjscore -= (int)score;
                    player.setXianWinScore((int)score);
                    winXPlayerList.add(player);
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("2".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}平赔", player.getWxId(), zh.getWxId());
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower((double)1);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore -= score;
                    player.setXianWinScore(score);
                    winXPlayerList.add(player);
                    ;
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("3".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                } else if ("5".equals(game.getMainTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == 1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                    } else {
                        logger.warn("闲家{}赢，庄家{}输", player.getWxId(), zh.getWxId());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            score = player.getBet() * player.getPower();
                        }
                        //int score = player.getBet() * player.getPower();
                        zjscore -= (int)score;
                        player.setXianWinScore((int)score);
                        winXPlayerList.add(player);
                        //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                    }
                }
                if (!"4".equals(game.getMainTimeout())) {
                    return zjscore;
                }
            } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy()) {
                logger.warn("闲家{}超时", player.getWxId());
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet() * zh.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(1.0);
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if ("5".equals(game.getPlayerTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == -1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                    } else {
                        logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                        player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            score = player.getBet() * zh.getPower();
                        }
                        //int score = player.getBet() * zh.getPower();
                        zjscore += (int)score;
                        player.setXianWinScore((int)score);
                        loserXPlayerList.add(player);
                        updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    }
                }
                if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            } else if (player.isProxy() && "N".equals(player.getIsExe())) {
                logger.warn("闲家{}超时", player.getWxId());
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet() * zh.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(1.0);
                    int score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += score;
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if ("5".equals(game.getPlayerTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == -1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                    } else {
                        logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                        player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            score = player.getBet() * zh.getPower();
                        }
                        // int score = player.getBet() * zh.getPower();
                        zjscore += (int)score;
                        updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    }
                }
                if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            }
            if (zh.getPoint() == player.getPoint()
                    && "Y".equals(powers.get(player.getPoint()).getIsDogfall())) {
                logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                return zjscore;
            }
            // 正常情况
            int result = PointHelper.compare(game, zh, player);
            if (result == 1) { // 庄赢
                logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                player.setPower(zh.getPower());
                double score = 0;
                if (player.isPoker()) {
                    player.setPower(1.0);
                    score = player.getBet();
                } else if (!player.isPoker()) {
                    score = player.getBet() * zh.getPower();
                }
                //int score = player.getBet() * zh.getPower();
                zjscore += (int)score;
                player.setXianWinScore((int)score);
                loserXPlayerList.add(player);
                updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
            } else if (result == -1) { // 闲赢
                logger.warn("闲家{}赢，庄家{}输", player.getWxId(), zh.getWxId());
                double score = 0;
                if (player.isPoker()) {
                    player.setPower(1.0);
                    score = player.getBet();
                } else if (!player.isPoker()) {
                    score = player.getBet() * player.getPower();
                }
                //int score = player.getBet() * player.getPower();
                zjscore -= (int)score;
                player.setXianWinScore((int)score);
                winXPlayerList.add(player);
                //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
            } else { // 打平
                logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                updatePlayerScore(tGame.getId(), player, resultList, 0, false);
            }
        }
        return zjscore;
    }

    public Integer playTeMa(Game game, Player player, Player zh, TGame tGame, List<TGamePlayer> resultList, boolean isUpdate, boolean isZhFirst, int zhScore, List<Player> winXPlayerList, boolean mainTimeout, List<Player> loserXPlayerList, Map<Integer, Power> powers) {
        Integer zjscore = zhScore;
        if ("2".equals(game.getOneFen()) && player.getAmount().doubleValue() == 0.01) {
            logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
            updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
            return zjscore;
        }
        if (isZhFirst && "1".equals(game.getMainFirst())) {
            logger.warn("闲家{}赢，庄家{}头包全赔", player.getWxId(), zh.getWxId());
            double score = 0;
            if (player.isPoker()) {//该闲家梭哈，只赔 百分比
                score = (int) (player.getBet() * Double.valueOf("0." + game.getPoker()));
            } else if (!player.isPoker()) {
                score = player.getBet() * player.getPower();
            }
            //int score = player.getBet() * player.getPower();
            zjscore -= (int)score;
            player.setXianWinScore((int)score);
            winXPlayerList.add(player);
            //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
        } else if (isZhFirst && "2".equals(game.getMainFirst())) {
            logger.warn("闲家{}赢，庄家{}头包平赔", player.getWxId(), zh.getWxId());
            int score = 0;
            if (player.isPoker()) {//该闲家梭哈，只赔 百分比
                score = (int) (player.getBet() * Double.valueOf("0." + game.getPoker()));
            } else if (!player.isPoker()) {
                score = player.getBet();
            }
            //int score = player.getBet();
            zjscore -= score;
            player.setXianWinScore(score);
            winXPlayerList.add(player);
            //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
        } else {
            // 超时处理
            if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy() && game.isZfreeBuy() && game.isXfreeBuy()) {
                logger.warn("闲家{}和庄家{}都超时", player.getWxId(), zh.getWxId());   //两家在自买的时候 ，判定都开启才能判定为两家都超时
                if ("1".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                } else if ("2".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        //score = player.getBet() * zh.getPower();
                        score = player.getBet();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(1.0);
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet() * 1;
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                }
                return zjscore;
            } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy() && !game.isZfreeBuy() && game.isXfreeBuy()) {
                logger.warn("闲家{}开启超时判定，庄家{}没有开启超时判定，闲家超时", player.getWxId(), zh.getWxId());   //两家在自买的时候 ，判定都开启才能判定为两家都超时
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        //score = player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList,(int)-score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(1.0);
                    int score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy() && game.isZfreeBuy() && !game.isXfreeBuy()) {
                logger.warn("闲家{}闲家没有开启超时判定，庄家{}开启超时判定，庄家超时", player.getWxId(), zh.getWxId());   //两家在自买的时候 ，判定都开启才能判定为两家都超时
                if ("1".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}全赔", player.getWxId(), zh.getWxId());
                    double score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * player.getPower();
                    zjscore -= (int)score;
                    player.setXianWinScore((int)score);
                    winXPlayerList.add(player);
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("2".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}平赔", player.getWxId(), zh.getWxId());
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore -= score;
                    player.setXianWinScore(score);
                    winXPlayerList.add(player);
                    ;
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("3".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                }
                if (!"4".equals(game.getMainTimeout())) {
                    return zjscore;
                }
            } else if (mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy()&& game.isZfreeBuy()) {
                logger.warn("庄家{}超时", zh.getWxId());
                if ("1".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}全赔", player.getWxId(), zh.getWxId());
                    double score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * player.getPower();
                    zjscore -= (int)score;
                    player.setXianWinScore((int)score);
                    winXPlayerList.add(player);
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("2".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}平赔", player.getWxId(), zh.getWxId());
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore -= score;
                    player.setXianWinScore(score);
                    winXPlayerList.add(player);
                    ;
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("3".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                } else if ("5".equals(game.getMainTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == 1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                    } else {
                        logger.warn("闲家{}赢，庄家{}输", player.getWxId(), zh.getWxId());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            score = player.getBet() * player.getPower();
                        }
                        //int score = player.getBet() * player.getPower();
                        zjscore -= (int)score;
                        player.setXianWinScore((int)score);
                        winXPlayerList.add(player);
                        //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                    }
                }
                if (!"4".equals(game.getMainTimeout())) {
                    return zjscore;
                }
            } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && game.isXfreeBuy()) {
                logger.warn("闲家{}超时", player.getWxId());
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        //score = player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList,(int)-score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(1.0);
                    int score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if ("5".equals(game.getPlayerTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == -1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                    } else {
                        logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                        //player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            //score = player.getBet() * zh.getPower();
                            score = player.getBet() * player.getPower();
                        }
                        //int score = player.getBet() * zh.getPower();
                        zjscore += (int)score;
                        player.setXianWinScore((int)score);
                        loserXPlayerList.add(player);
                        updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    }
                }
                if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            } else if (player.isProxy() && "N".equals(player.getIsExe()) && game.isXfreeBuy()) {
                logger.warn("闲家{}超时", player.getWxId());
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        //score =  player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    updatePlayerScore(tGame.getId(), player, resultList,(int) -score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(1.0);
                    int score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += score;
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if ("5".equals(game.getPlayerTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == -1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                    } else {
                        logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                        //player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            //score = player.getBet() * zh.getPower();
                            score = player.getBet() * player.getPower();
                        }
                        // int score = player.getBet() * zh.getPower();
                        zjscore += (int)score;
                        updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    }
                }
                if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            }
            // 正常情况
            Player resultPlayer = PointHelper.compare(player);
            double lScore = 0;
            for (BetPower power :
                    resultPlayer.getBetPowerList()) {
                Integer result = power.getWin();
                if (result == 1) {//猜错
                    lScore -= power.getBet();
                } else if (result == -1) {//猜对
                    lScore += power.getBet() * power.getPower() - power.getBet();
                } else {//抓合g
                    if (game.getTmCatchUs().equals("1")) {
                        lScore -= 0;
                    } else if (game.getTmCatchUs().equals("2")) {
                        lScore -= power.getBet() / 2;
                    } else {
                        lScore -= power.getBet();
                    }

                }
            }
            if (lScore <= 0) { // 庄赢  lScore 是带有正负值得。
                logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                //player.setPower(zh.getPower());
                double score = 0;
                score = -lScore;
                //int score = player.getBet() * zh.getPower();
                zjscore += (int)score;
                player.setXianWinScore((int)score);
                loserXPlayerList.add(player);
                updatePlayerScore(tGame.getId(), player, resultList, (int)-score, true);
            } else if (lScore > 0) { // 闲赢
                logger.warn("闲家{}赢，庄家{}输", player.getWxId(), zh.getWxId());
                double score = 0;
                score = lScore;
                //int score = player.getBet() * player.getPower();
                zjscore -= (int)score;
                player.setXianWinScore((int)score);
                winXPlayerList.add(player);
                //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
            } else { // 玩家抓合
                logger.warn("玩家{}抓合", player.getWxId());//1.抓合不赔；2.抓合赔一半；3.抓合全赔
                if (game.getCatchUs().equals("1")) {//抓合不赔
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if (game.getCatchUs().equals("2")) {//抓合赔一半
                    logger.warn("闲家{}输，庄家{}赢，赔一半", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    score = player.getBet() * player.getPower() / 2;
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if (game.getCatchUs().equals("3")) {//抓合全赔
                    logger.warn("闲家{}输，庄家{}赢，全赔", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
//                        score = player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                }
            }
        }
        return zjscore;
    }

    public Integer playBJL(Game game, Player player, Player zh, TGame tGame, List<TGamePlayer> resultList, boolean isUpdate, boolean isZhFirst, int zhScore, List<Player> winXPlayerList, boolean mainTimeout, List<Player> loserXPlayerList, Map<Integer, Power> powers) {
        Integer zjscore = zhScore;
        if ("2".equals(game.getOneFen()) && player.getAmount().doubleValue() == 0.01) {
            logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
            updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
            return zjscore;
        }
        if (isZhFirst && "1".equals(game.getMainFirst())) {
            logger.warn("闲家{}赢，庄家{}头包全赔", player.getWxId(), zh.getWxId());
            double score = 0;
            if (player.isPoker()) {//该闲家梭哈，只赔 百分比
                score = (int) (player.getBet() * Double.valueOf("0." + game.getPoker()));
            } else if (!player.isPoker()) {
                score = player.getBet() * player.getPower();
            }
            //int score = player.getBet() * player.getPower();
            zjscore -= (int)score;
            player.setXianWinScore((int)score);
            winXPlayerList.add(player);
            //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
        } else if (isZhFirst && "2".equals(game.getMainFirst())) {
            logger.warn("闲家{}赢，庄家{}头包平赔", player.getWxId(), zh.getWxId());
            int score = 0;
            if (player.isPoker()) {//该闲家梭哈，只赔 百分比
                score = (int) (player.getBet() * Double.valueOf("0." + game.getPoker()));
            } else if (!player.isPoker()) {
                score = player.getBet();
            }
            //int score = player.getBet();
            zjscore -= score;
            player.setXianWinScore(score);
            winXPlayerList.add(player);
            //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
        } else {
            // 超时处理
            if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy() && game.isZfreeBuy() && game.isXfreeBuy()) {
                logger.warn("闲家{}和庄家{}都超时", player.getWxId(), zh.getWxId());   //两家在自买的时候 ，判定都开启才能判定为两家都超时
                if ("1".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                } else if ("2".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        //score = player.getBet() * zh.getPower();
                        score = player.getBet();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getDoubleTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(1.0);
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet() * 1;
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                }
                return zjscore;
            } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy() && !game.isZfreeBuy() && game.isXfreeBuy()) {
                logger.warn("闲家{}开启超时判定，庄家{}没有开启超时判定，闲家超时", player.getWxId(), zh.getWxId());   //两家在自买的时候 ，判定都开启才能判定为两家都超时
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        //score = player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(1.0);
                    int score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy() && game.isZfreeBuy() && !game.isXfreeBuy()) {
                logger.warn("闲家{}闲家没有开启超时判定，庄家{}开启超时判定，庄家超时", player.getWxId(), zh.getWxId());   //两家在自买的时候 ，判定都开启才能判定为两家都超时
                if ("1".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}全赔", player.getWxId(), zh.getWxId());
                    double score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * player.getPower();
                    zjscore -= (int)score;
                    player.setXianWinScore((int)score);
                    winXPlayerList.add(player);
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("2".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}平赔", player.getWxId(), zh.getWxId());
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore -= score;
                    player.setXianWinScore(score);
                    winXPlayerList.add(player);
                    ;
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("3".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                }
                if (!"4".equals(game.getMainTimeout())) {
                    return zjscore;
                }
            } else if (mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy()&& game.isZfreeBuy()) {
                logger.warn("庄家{}超时", zh.getWxId());
                if ("1".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}全赔", player.getWxId(), zh.getWxId());
                    double score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * player.getPower();
                    zjscore -= (int)score;
                    player.setXianWinScore((int)score);
                    winXPlayerList.add(player);
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("2".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}赢，庄家{}平赔", player.getWxId(), zh.getWxId());
                    int score = 0;
                    if (player.isPoker()) {
                        score = player.getBet();
                        player.setPower(1.0);
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore -= score;
                    player.setXianWinScore(score);
                    winXPlayerList.add(player);
                    ;
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else if ("3".equals(game.getMainTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                } else if ("5".equals(game.getMainTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == 1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                    } else {
                        logger.warn("闲家{}赢，庄家{}输", player.getWxId(), zh.getWxId());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            score = player.getBet() * player.getPower();
                        }
                        //int score = player.getBet() * player.getPower();
                        zjscore -= (int)score;
                        player.setXianWinScore((int)score);
                        winXPlayerList.add(player);
                        //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                    }
                }
                if (!"4".equals(game.getMainTimeout())) {
                    return zjscore;
                }
            } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                    && !player.isProxy() && game.isXfreeBuy()) {
                logger.warn("闲家{}超时", player.getWxId());
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        //score = player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(1.0);
                    int score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += score;
                    player.setXianWinScore(score);
                    loserXPlayerList.add(player);
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if ("5".equals(game.getPlayerTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == -1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                    } else {
                        logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                        //player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            //score = player.getBet() * zh.getPower();
                            score = player.getBet() * player.getPower();
                        }
                        //int score = player.getBet() * zh.getPower();
                        zjscore += (int)score;
                        player.setXianWinScore((int)score);
                        loserXPlayerList.add(player);
                        updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    }
                }
                if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            } else if (player.isProxy() && "N".equals(player.getIsExe()) && game.isXfreeBuy()) {
                logger.warn("闲家{}超时", player.getWxId());
                if ("1".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    //player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        //score =  player.getBet() * zh.getPower();
                        score = player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zjscore += (int)score;
                    updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if ("2".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(1.0);
                    int score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet();
                    }
                    //int score = player.getBet();
                    zjscore += score;
                    updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                } else if ("3".equals(game.getPlayerTimeout())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                } else if ("5".equals(game.getPlayerTimeout())) {
                    int result = PointHelper.compare(game, zh, player);
                    if (result == -1) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                    } else {
                        logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                        //player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            //score = player.getBet() * zh.getPower();
                            score = player.getBet() * player.getPower();
                        }
                        // int score = player.getBet() * zh.getPower();
                        zjscore += (int)score;
                        updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    }
                }
                if (!"4".equals(game.getPlayerTimeout())) {
                    return zjscore;
                }
            }
            // 正常情况
            int result = PointHelper.compare(game, zh,player,"");
            if (result == 1) { // 庄赢
                logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                //player.setPower(zh.getPower());
                int score = 0;
                if (player.isPoker()) {
                    player.setPower(1.0);
                    score = player.getBet();
                } else if (!player.isPoker()) {
//                        score = player.getBet() * zh.getPower();
                    score = player.getBet();
                }
                //int score = player.getBet() * zh.getPower();
                zjscore += score;
                player.setXianWinScore(score);
                loserXPlayerList.add(player);
                updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
            } else if (result == -1) { // 闲赢
                logger.warn("闲家{}赢，庄家{}输", player.getWxId(), zh.getWxId());
                double score = 0;
                if (player.isPoker()) {
                    player.setPower(1.0);
                    score = player.getBet();
                } else if (!player.isPoker()) {
                    score = player.getBet() * player.getPower();
                }
                //int score = player.getBet() * player.getPower();
                zjscore -= (int)score;
                player.setXianWinScore((int)score);
                winXPlayerList.add(player);
                //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
            }
        }
        return zjscore;
    }

}
